package com.my.mall.manage.order.controller.orderInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.mall.common.R;
import com.my.mall.manage.order.dto.orderInfo.OrderInfoManageDTO;
import com.my.mall.manage.order.service.orderInfo.OrderInfoManageService;
import com.my.mall.manage.order.vo.orderInfo.OrderInfoManageVo;
import com.my.mall.manage.user.service.UserManageService;
import com.my.mall.manage.user.vo.UserInfoVo;

@Controller
@RequestMapping("manage/orderInfo")
public class OrderInfoManageController {
	@Autowired
	OrderInfoManageService orderInfoManageService;

	@Autowired
	UserManageService userManageService;

	@RequestMapping("selectOrderInfoList")
	@ResponseBody
	public Map<String, Object> selectOrderInfoList(int orderStatus, int iDisplayStart, int iDisplayLength) {
		List<OrderInfoManageVo> list = orderInfoManageService.selectOrderInfoList(orderStatus, iDisplayStart,
				iDisplayLength);
		for (OrderInfoManageVo orderInfo : list) {
			UserInfoVo user = userManageService.getUser(orderInfo.getUserId());
			if (user != null) {
				orderInfo.setNickName(user.getNickName());
			}
		}
		int size = orderInfoManageService.selectOrderInfoCount(orderStatus);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	@RequestMapping("save")
	@ResponseBody
	public R save(OrderInfoManageDTO orderInfo) {
		int result = 0;
		if (orderInfo.getId() == 0)
			result = orderInfoManageService.save(orderInfo);
		else
			result = orderInfoManageService.update(orderInfo);

		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	@RequestMapping("getOrderInfo/{id}")
	@ResponseBody
	public R getOrderInfo(@PathVariable("id") int id) {
		OrderInfoManageVo orderInfoDate = orderInfoManageService.getOrderInfo(id);
		return R.ok().put("orderInfoDate", orderInfoDate);
	}

	@RequestMapping("del/{id}")
	@ResponseBody
	public R del(@PathVariable("id") int id) {
		int result = orderInfoManageService.del(id);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障");
	}

	@RequestMapping("selectOrderRefundList")
	@ResponseBody
	public Map<String, Object> selectOrderRefundList(int isRefund, int iDisplayStart, int iDisplayLength) {
		List<OrderInfoManageVo> list = orderInfoManageService.selectOrderRefundList(isRefund, iDisplayStart,
				iDisplayLength);
		int size = orderInfoManageService.selectOrderRefundCount(isRefund);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	@RequestMapping("updateByRefund")
	@ResponseBody
	public R updateByRefund(int id, int isRefund, String refundMessage) {
		int result = 0;
		result = orderInfoManageService.updateByRefund(id, isRefund, refundMessage);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

}